package org.example.rocketmq.a01_quickstart;

import org.apache.commons.lang3.ThreadUtils;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.example.rocketmq.NamesrvAddr;

import java.time.Duration;

public class ProducerOneway {

    public static void main(String[] args) throws Exception {
        onewayProducer();
    }

    /**
     * 单向发送：只负责发送， 不管消息是否发送成功
     */
    public static void onewayProducer() throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("producer_oneway_group_01");
        producer.setNamesrvAddr(NamesrvAddr.get());
        producer.setSendMsgTimeout(15000); // 设置超时时间 15秒
        producer.start();

        for (int i = 0; i < 2; i++) {
            try {
                String body = ("Hello RocketMQ Oneway" + i);
                System.out.println(body);
                Message message = new Message("TopicTest", "TagA", body.getBytes(RemotingHelper.DEFAULT_CHARSET));

                producer.sendOneway(message);
                System.out.printf(i + "消息发送成功%n");
            } catch (Exception e) {
                e.printStackTrace();
                ThreadUtils.sleep(Duration.ofSeconds(1));
            }
        }

        producer.shutdown();
    }
}
